export default {
    namespaced: true,
    state: {
       cartList:uni.getStorageSync("cartList") ||[]
    },
    getters: {
        //已选商品
        checkedCartList(state){
            return state.cartList.filter(item=>item.checked===true)
        },
        //要支付的商品总价
        checkedPrice(state,getters){
            return getters.checkedCartList.reduce((price,item)=>price+=item.nums * item.goods_price, 0)
        },
        //要支付的商品数量
        checkedCount(state,getters){
            return getters.checkedCartList.reduce((count,item)=>count+=item.nums,0)
        }
    },
    mutations: {
        //添加购物车
         cartAdd(state,payload){
             const index = state.cartList.findIndex(item=>item.goods_id===payload.value.goods_id)
             if(index===-1){
                 state.cartList.push({...payload.value,nums:1,checked:true})
             }else{
                 state.cartList[index].nums++
             }
             uni.setStorageSync("cartList",state.cartList)
         },
         //购物车checked事件
         cartGoodsChecked(state,payload){
             state.cartList[payload.index].checked=!state.cartList[payload.index].checked
             uni.setStorageSync("cartList",state.cartList)
         },
         //购物车数量点击事件
         cartGoodsNumUpdate(state,payload){
             state.cartList[payload.index].nums += payload.unit
             uni.setStorageSync("cartList",state.cartList)
         },
        //  购物车商品删除事件
         cartGoodsRemove(state,payload){
             state.cartList.splice(payload.index,1)
             uni.setStorageSync("cartList",state.cartList)
         },
         //购物车全选影响单选事件
         cartAllGoodsChecked(state,payload){
             state.cartList.forEach(item=>item.checked=payload.checked)
             uni.setStorageSync("cartList",state.cartList)
         }
    },
    actions: { 

    }
}